// pages/entry/entry.js
import Dialog from '@vant/weapp/dialog/dialog';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    entryType:'question',
    questionType:'HTML',
    question:null,
    answer:null,
    diffculty:0,
    word:null,
    wordType:'HTML',
    explain:null
  },
  radioChange(e){
    console.log(e.detail.value);
    this.setData({
      entryType:e.detail.value
    })
  },
  getQuestionType(e){
    this.setData({
      questionType:e.detail.value
    })
  },
  getWordType(e){
    this.setData({
      wordType:e.detail.value
    })
  },
  getQuestion(e){
    // console.log(e.detail.value);
    this.setData({
      question:e.detail.value
    })
  },
  getAnswer(e){
    this.setData({
      answer:e.detail.value
    })
  },
  onChange(e){
    this.setData({
      diffculty:e.detail
    })
  },
  getWord(e){
    this.setData({
      word:e.detail.value
    })
  },
  getExplain(e){
    this.setData({
      explain:e.detail.value
    })
  },
  submit(){
    if(!app.globalData.token){
      Dialog.alert({
        message: '请先登录',
      }).then(() => {
        // on close
      });
      return
    }
    if(this.data.entryType==='question'){
      console.log(this.data.diffculty,this.data.questionType,this.data.question,this.data.answer);
      if(!this.data.question){
        Dialog.alert({
          message: '请输入题目',
        }).then(() => {
          // on close
        });
      }else if(!this.data.answer){
        Dialog.alert({
          message: '请输入答案',
        }).then(() => {
          // on close
        });
      }else if(this.data.diffculty===0){
        Dialog.alert({
          message: '请输入难度',
        }).then(() => {
          // on close
        });
      }else{
        const db = wx.cloud.database();
        let length = 0;
        db.collection('questions').count().then(
          res=>{
            console.log(res.total);
            length = res.total
            db.collection('questions').add({
              data:{
                answer:this.data.answer,
                difficulty:this.data.diffculty,
                order: length+1,
                title:this.data.question,
                type:this.data.questionType
              }
            }).then(
              res=>{
                Dialog.alert({
                  message: '录入成功！',
                }).then(() => {
                  // on close
                });
              }
            )
          }
        )
        
      }
    }else{
      if(!this.data.word){
        Dialog.alert({
          message: '请输入单词',
        }).then(() => {
          // on close
        });
      }else if(!this.data.explain){
        Dialog.alert({
          message: '请输入单词大意',
        }).then(() => {
          // on close
        });
      }else{
        const db = wx.cloud.database();
        let length = 0;
        db.collection('words').count().then(
          res=>{
            console.log(res.total);
            length = res.total
            db.collection('words').add({
              data:{
                meaning:this.data.explain,
                type:this.data.wordType,
                word:this.data.word
              }
            }).then(
              res=>{
                Dialog.alert({
                  message: '录入成功！',
                }).then(() => {
                  // on close
                });
              }
            )
          }
        )
      }
    }
  },
  reset(){
    this.setData({
      question:null,
      answer:null,
      diffculty:0,
      word:null,
      explain:null
    })
  }
})